package com.fwj.core.common.common;

import com.alibaba.fastjson.JSONObject;
import com.fwj.core.common.model.DataBuriedPointLogBean;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;

/**
 * @Author: fwj
 * @Data: 2022/4/16 22:09
 * @Version 1.0
 * @Description:
 */


public class LogUtils {

    public static DataBuriedPointLogBean buildDataBuriedPointLog(HttpServletRequest request) {
        DataBuriedPointLogBean dataBuriedPointLogBean = new DataBuriedPointLogBean();
        dataBuriedPointLogBean.setStartTime(new Timestamp(System.currentTimeMillis()));
        dataBuriedPointLogBean.setScheme(request.getScheme());
        dataBuriedPointLogBean.setProtocol(request.getProtocol());
        dataBuriedPointLogBean.setServerName(request.getServerName());
        dataBuriedPointLogBean.setServerPort(request.getServerPort());
        dataBuriedPointLogBean.setLocalAddr(request.getLocalAddr());
        dataBuriedPointLogBean.setLocalName(request.getLocalName());
        dataBuriedPointLogBean.setLocalPort(request.getLocalPort());
        dataBuriedPointLogBean.setMethod(request.getMethod());
        dataBuriedPointLogBean.setRequestURI(request.getRequestURI());
        dataBuriedPointLogBean.setRequestURL(request.getRequestURL());
        dataBuriedPointLogBean.setRealIP(request.getHeader("X-Real-IP"));
        dataBuriedPointLogBean.setRealHost(request.getHeader("Host"));
        dataBuriedPointLogBean.setRealAddr(request.getHeader("X-Forwarded-For"));
        dataBuriedPointLogBean.setCookie(JSONObject.toJSONString(CookieUtil.getAllCookie(request)));
        return dataBuriedPointLogBean;
    }

}
